package com.xiaoye.qbdatahandle.controller;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.fastjson.JSONObject;
import com.xiaoye.qbdatahandle.domian.wiki.ContentInfo;
import com.xiaoye.qbdatahandle.util.StringUtils;

public class ContentInfoExcelConverter implements Converter<ContentInfo> {
    @Override
    public Class<ContentInfo> supportJavaTypeKey() {
        return ContentInfo.class;
    }

    @Override
    public ContentInfo convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (cellData == null || StringUtils.isBlank(cellData.getStringValue())) {
            return null;
        }
        return JSONObject.parseObject(cellData.getStringValue(), ContentInfo.class);
    }

    @Override
    public WriteCellData<?> convertToExcelData(ContentInfo value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == null) {
            return null;
        }
        return new WriteCellData<>(JSONObject.toJSONString(value));
    }
}
